package tq.com.tqteacher.mine.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.donkingliang.imageselector.utils.ImageSelector;

import java.util.ArrayList;

import tq.com.tqcom.base.BaseFragment;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqcom.util.TQLoadingHelper;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqteacher.R;
import tq.com.tqteacher.mine.activity.TQInputActivity;
import tq.com.tqteacher.mine.activity.TQModifyPhonePwdInfoActivity;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.propertyEnum.TQModify;

import static android.app.Activity.RESULT_OK;
import static android.content.Context.RECEIVER_VISIBLE_TO_INSTANT_APPS;

public class TQPersonSettingFragment extends BaseFragment {
    private TextView mNickName;
    private ImageView headerIcon;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View lView = inflater.inflate(R.layout.fragment_mineset_layout, container, false);

        lView.findViewById(R.id.phone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoModify(0);
            }
        });

        lView.findViewById(R.id.modify_pwd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoModify(1);
            }
        });

        lView.findViewById(R.id.icon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                choicePic();
            }
        });
        lView.findViewById(R.id.nick).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoInput();
            }
        });

        TextView xueke = lView.findViewById(R.id.xueke);
        xueke.setText(TQSystemDefine.subjectTitle(TQLogin.getCurrent().getUserInfo().getSubject()));

        TextView grade = lView.findViewById(R.id.nianji);
        grade.setText(TQSystemDefine.gradeName(TQLogin.getCurrent().getUserInfo().getGrade()));

        mNickName = lView.findViewById(R.id.nick_name);
        headerIcon = lView.findViewById(R.id.header_icon);

        RequestOptions lOptions = new RequestOptions().error(R.drawable.tx1)
                .placeholder(R.drawable.tx1).circleCrop();

        String url = TQPictureUpload.downloadPath(TQLogin.getCurrent().getUserInfo().getPic());
        Glide.with(this).load(url).apply(lOptions).into(headerIcon);

        mNickName.setText(TQLogin.getCurrent().getUserInfo().getName());
        return lView;
    }

    private void gotoModify(int key){
        Intent intent = new Intent(this.getContext(), TQModifyPhonePwdInfoActivity.class);
        intent.putExtra("index", key);
        startActivity(intent);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode != RESULT_OK || data == null){
            return;
        }

        if(requestCode == 0x01){
            ArrayList<String> files = data.getStringArrayListExtra(ImageSelector.SELECT_RESULT);
            if(files != null && files.size() > 0){
                String fileId = files.get(0);
                uploadPic(fileId);
            }
        }else if(requestCode == 0x02){
            String content = data.getStringExtra(TQInputActivity.RESULT_CONTENT);
            modifyName(content);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void choicePic(){
        ImageSelector.builder()
                .setCrop(true)
                .setSingle(true)
                .start(this, 0x01);
    }

    private void uploadPic(final String f){
        TQLoadingHelper.build(this.getContext()).setCancelable(false).show();
        TQPictureUpload.uploadPicture(f, new INetCallback() {
            @Override
            public void result(TQNetResult pNetResult) {
                if(!pNetResult.isSuccess()){
                    TQLoadingHelper.setError(pNetResult.getErrMsg());
                    return;
                }

                String fId = TQPictureUpload.uploadResult(pNetResult);
                if(fId == null){
                    TQLoadingHelper.setError("后台返回失败");
                    return;
                }
                modifyPic(fId);
            }
        });
    }

    private void modifyPic(final String fid){
        TQNetInterface.modifyTeacherInfo(TQLogin.getCurrent().getUserInfo().getId(), TQModify.TeacherInfo.pic, fid, new INetCallback() {
            @Override
            public void result(TQNetResult pNetResult) {
                if(!pNetResult.isSuccess()){
                    TQLoadingHelper.setError(pNetResult.getErrMsg());
                    return;
                }

                RequestOptions lOptions = new RequestOptions().error(R.drawable.tx1)
                        .placeholder(R.drawable.tx1).circleCrop();

                String url = TQPictureUpload.downloadPath(fid);
                Glide.with(TQPersonSettingFragment.this).load(url).apply(lOptions).into(headerIcon);
                TQLogin.getCurrent().getUserInfo().setPic(fid);

                TQLoadingHelper.dimiss();
            }
        });
    }
    private void modifyName(final String name){
        TQNetInterface.modifyTeacherInfo( TQLogin.getCurrent().getUserInfo().getId(),TQModify.TeacherInfo.name, name, new INetCallback() {
            @Override
            public void result(TQNetResult pNetResult) {
                if(!pNetResult.isSuccess()){
                    TQLoadingHelper.setError(pNetResult.getErrMsg());
                    return;
                }

                mNickName.setText(name);
                TQLogin.getCurrent().getUserInfo().setName(name);
                TQLoadingHelper.dimiss();
            }
        });
    }

    private void gotoInput(){
        TQInputActivity.show(this, 0x02, "昵称", "请输入您的昵称", 16);
    }
}
